$(function() {
	// Dialog
	var id = $( "#idLDAP" ),
	password = $("#clearPassword"),
	allFields = $( [] ).add( id ).add( password ),
	tips = $( ".validateTips" ),
	single=true;
	   
	   $( "#login-dialog" ).dialog({
		autoOpen: false,
		height: 380,
		minHeight: 380,
		width: 380,
		minWidth: 380,
		modal: true,
		close: function() {
			allFields.val( "" ).removeClass( "ui-state-error" );
			$( "#login-dialog" ).children().remove();
		},
		buttons: [{
				id:"cancel",
				tabindex:4,
				text:"Annuler",
				click: function() {
							$( this ).dialog( "close" );
							$( "#login-dialog" ).children().remove();
						}
			},
			{
				id:"submit-login",
				tabindex:3,
				text:"Se connecter",
				click: function() {
					
					var bValid = true;
					
					allFields.removeClass( "ui-state-error" );
	
					bValid = bValid && checkLength( tips, id, "Identifiant", 3, 16 );
					//bValid = bValid && checkLength( tips, email, "email", 6, 80 );
					bValid = bValid && checkLength( tips, password, "Mot de passe", 6, 16 );
	
					//bValid = bValid && checkRegexp( tips, name, /^[a-z]([0-9a-z_])+$/i, "Username may consist of a-z, 0-9, underscores, begin with a letter." );
					// From jquery.validate.js (by joern), contributed by Scott Gonzalez: http://projects.scottsplayground.com/email_address_validation/
					//bValid = bValid && checkRegexp( tips, email, /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i, "eg. ui@jquery.com" );
					//bValid = bValid && checkRegexp( tips, password, /^([0-9a-zA-Z])+$/, "Password field only allow : a-z 0-9" );
	
					if ( bValid&&single ) {
						single=false;
						$( "#login-dialog" ).append("<div id='progress'><br/><p class='progressbar'>Connexion en cours..</p><div id='progressbar' class='progressbar'/></div></div>");
						$('#encryptedPassword').val ( hex_sha1($('#clearPassword').val()));
						$( "#progressbar" ).progressbar({
							value: 100
						});
						
						$.ajax({
							type: 'POST',
							dataType:'json',
							url: "login.htm",
							data: {
								"idLDAP": $( "#idLDAP" ).val(),
								"password": $( "#encryptedPassword" ).val()
							},
							error:function(msg){
								tips.text('error').addClass('ui-state-error ui-corner-all');
								$( "#progress" ).remove();
								single=true;
							},
							success: function( data ) {
								single=true;
								if(data.success==true){
									$( "#login-dialog"  ).dialog( "close" );
									$( "#notification-reload-dialog" ).append("<p style='text-align=center;'><img src='/ASOF/img/success.png' style='width:20px ;position: absolute; left: .5em; top: 13%;margin-top: -8px;' /><span style='position: absolute;left:30px'>"+data.message+"</span></p>");
									$( "#notification-reload-dialog" ).dialog("open");
									
								}else{
									tips.text(data.message).addClass('ui-state-error ui-corner-all');
									$( "#progress" ).remove();
								}
									
									
							}
						});
						
					}
				}
			}
		]
	});
});